<?php
$addclass = new AddClass;

class AddClass {

	function AddClass () {
		global $setupdata, $mysqlclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
	}
		
	function replase_value($from, $to){
		$rows = array();
		$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."data` where `value` LIKE '%".$from."%'", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) array_push($rows, $row);
		foreach ($rows as $row){
			$str = ereg_replace($from, $to, $row['value']);
			$str = addslashes($str);
			$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."data` SET value=\"".$str."\" WHERE `id` = ".$row['id'], __LINE__, __FILE__);
		}
		$rows = array();
		$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."catalog` where `dlink` LIKE '%".$from."%'", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) array_push($rows, $row);
		foreach ($rows as $row){
			$str = ereg_replace($from, $to, $row['dlink']);
			$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."catalog` SET dlink='".$str."' WHERE `id` = ".$row['id'], __LINE__, __FILE__);
		}
	}
	
	function clear_lt(){
		$rows = array();
		$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."data` where `name` = 'product01_cost' and `value` LIKE '% Lt%'", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) array_push($rows, $row);
		$row = array();
		foreach ($rows as $row){
			$value = preg_replace("/[A-Z]+|[a-z]+|\s+/", "", $row['value']);
			$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."data` SET value='".$value."' WHERE `id` = ".$row['id'], __LINE__, __FILE__);
		}
	}
	
	function repair_lang2($parent, $lang){
		if (!isset($parent) or (strlen($parent) < 1)) return NULL;
		$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."catalog` SET `lang`='".$lang."' WHERE `parent`='".$parent."'", __LINE__, __FILE__);
		$sql = $this->mysqlclass->mysql_my_query("SELECT `id`, `lang` FROM `".$this->setupdata['table_prefix']."catalog` WHERE `parent`=".$parent, __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql)){	
			$this->repair_lang2($row['id'], $row['lang']);
		}
	}
	
	function repair_lang(){
		$sql = $this->mysqlclass->mysql_my_query("SELECT `id`, `lang` FROM `".$this->setupdata['table_prefix']."catalog` WHERE `parent`=0", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql)){	
			$this->repair_lang2($row['id'], $row['lang']);
		}
	}
	
	function delete_from_tree_childs_by_parent_type_sub($type, $value){
		if ($value['type'] != $type) {
			if (!is_array($value['parent'])) return 1;
			else return $this->delete_from_tree_childs_by_parent_type_sub($type, $value['parent']);
		}
		return 0;
	}

	function delete_from_tree_childs_by_parent_type($type, $tree){
		$new_tree = array();
		foreach($tree as $key=>$value){
			if ($value['type'] != $type) {
				if (!is_array($value['parent'])) $new_tree[$key] = $value;
				else if ($this->delete_from_tree_childs_by_parent_type_sub($type, $value) == 1) $new_tree[$key] = $value;
			}
		}
		return $new_tree;
	}
	
	function corect_shop_pos($id) {
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE `parent`=".$id, __LINE__, __FILE__);
		$pos = 1;
		if (mysql_num_rows($sql) > 0){
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
				$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_catalog` SET `pos`=".$pos." WHERE `id` = ".$row['id'], __LINE__, __FILE__);
				$pos = $pos + 1;
			}
		} else return false;
	}
}
?>